﻿#region Creditos

/* Autor: Edilberto Sánchez Forero	*/
/* Email: esanchezfo@gmail.com		*/
/* Licencia: LGPL, Software Libre	*/
/* País: Colombia, Bogotá			*/

#endregion

#region Librerias

using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.Serialization;

#endregion


namespace ESF.General.Entity
{
	[DataContract]
	[Serializable]
	public class LeafTreeItem<O> where O : IEquatable<O>
	{
		#region Miembros
		
		public O Content;
		
		public List<LeafTreeItem<O>> Children;
		
		#endregion
		
		
		#region Propiedades
		
		public int AllChildrenCount
		{
			get
			{
				int result = 0;
			
			
				if (Children != null) 
				{
					foreach (LeafTreeItem<O> child in Children)
					{
						result += child.AllChildrenCount;
					}
				}
					
				return result;
			}
		}
		
		#endregion
		
		
	    #region Constructor

		private LeafTreeItem () {}


		public LeafTreeItem (O pContent)
		{
			Content = pContent;
			Children = new List<LeafTreeItem<O>> ();
		}
		
		#endregion
		
		
		#region Funciones
	    
	    public List<O> GetAllChildren ()
	    {
			List<O> results;
	    
	    
			results = new List<O> ();
	    
			if (Children == null || Children.Count == 0) { return results; }
			
			AddChildren (ref results);
			
			return results;
	    }
	    
	    
	    private void AddChildren (ref List<O> results)
	    {
			foreach (LeafTreeItem <O> item in Children)
			{
				item.AddChildren (ref results);
				
				results.Add (item.Content);
			}
	    }
	    
	    
	    public void Display (int pintLevel, ref StringBuilder pstbResult)
		{
			if (Content != null)
			{		
				pstbResult.AppendFormat ("{0} {1}\n", new String('-', pintLevel), Content);
			}
		}
		
		
		public void Add (O pObject)
		{
			Children.Add (new LeafTreeItem<O> (pObject));
		}
		
		
		public void Remove (O pObject)
		{
			Children.Remove (new LeafTreeItem<O> (pObject));
		}

		#endregion
	}
}
